package com.itheima.service.impl.orderSetting;

import com.itheima.dao.OrderSetting.OrderSettingDao;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.orderSetting.OrderSettingService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 陈英航
 * @since 2021/11/6 14:04
 */
@Service
public class OrderSettingServiceImpl implements OrderSettingService {

    @Autowired
    private OrderSettingDao orderSettingDao;

    /**
     * 添加
     * @param list
     */
    @Override
    public void add(List<OrderSetting> list) {
        if (list != null && list.size() > 0) {

            for (OrderSetting orderSetting : list) {
                long orderDate = orderSettingDao.findOrderDate(orderSetting.getOrderDate());

                if (orderDate > 0) {
                    orderSettingDao.update(orderSetting);
                } else {
                    orderSettingDao.add(orderSetting);
                }
            }

        }
    }

    /**
     * 查询预约人数
     * @param date
     * @return
     */
    @Override
    public List<Map> getOrderSettingByMonth(String date) {
        String dateBegin = date+"-1";
        String dateEnd = date+"-31";
        Map map = new HashMap();
        map.put("dateBegin",dateBegin);
        map.put("dateEnd",dateEnd);
        List<OrderSetting> orderSettingByMonth = orderSettingDao.getOrderSettingByMonth(map);
        List<Map> data = new ArrayList<>();

        for (OrderSetting orderSetting : orderSettingByMonth) {
            Map orderSettingMap = new HashMap();
            //获得日期（几号）
            orderSettingMap.put("date",orderSetting.getOrderDate().getDate());
            //可预约人数
            orderSettingMap.put("number",orderSetting.getNumber());
            //已预约人数
            orderSettingMap.put("reservations",orderSetting.getReservations());
            data.add(orderSettingMap);
        }


        return data;
    }


    /**
     * 修改
     * @param orderSetting
     */
    @Override
    public void editNumberByDate(OrderSetting orderSetting) {
        long orderDate = orderSettingDao.findOrderDate(orderSetting.getOrderDate());

        if(orderDate > 0){
            //当前日期已经进行了预约设置，需要进行修改操作
            orderSettingDao.update(orderSetting);
        }else{
            //当前日期没有进行预约设置，进行添加操作
            orderSettingDao.add(orderSetting);
        }
    }


}
